package com.hdl.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseClientDemo {
    private Configuration conf = null;
    private Connection conn = null;

    @Before
    public void init() throws IOException {
        // 获取一个配置对象
        conf = HBaseConfiguration.create();

        // 设置zk参数
        conf.set("hbase.zookeeper.quorum", "linux142,linux143,linux144");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        // 通过conf获取到hbase集群连接
        conn = ConnectionFactory.createConnection(conf);

    }


    //创建一张hbase表, 关系表
    @Test
    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        HTableDescriptor worker = new HTableDescriptor(TableName.valueOf("user_relations"));
        //指定列族
        worker.addFamily(new HColumnDescriptor("friends"));
        admin.createTable(worker);
        System.out.println("好友表表创建成功！！");
    }

    // 插入关系数据
    @Test
    public void putRelationData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("user_relations"));

        List<Put> puts = new ArrayList<>();
        // rowkey  uid1
        Put put = new Put(Bytes.toBytes("uid1"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid2"),Bytes.toBytes("uid2"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid5"),Bytes.toBytes("uid5"));
        put.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid7"),Bytes.toBytes("uid7"));
        puts.add(put);

        // rowkey uid2
        Put put2 = new Put(Bytes.toBytes("uid2"));
        put2.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid1"),Bytes.toBytes("uid1"));
        put2.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid3"),Bytes.toBytes("uid3"));
        put2.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid6"),Bytes.toBytes("uid6"));
        put2.addColumn(Bytes.toBytes("friends"),Bytes.toBytes("uid10"),Bytes.toBytes("uid10"));
        puts.add(put2);

        table.put(puts);

        table.close();
        System.out.println("插入数据到relations表成功！！");
    }


    @Test
    public void delRelationData() throws IOException {
        Table table = conn.getTable(TableName.valueOf("user_relations"));

        // rowkey  uid1
        Delete delete = new Delete(Bytes.toBytes("uid1"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"));

        table.delete(delete);

        table.close();
        System.out.println("删除relations表数据成功！！");
    }

    @After
    public void release(){
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
